package com.dhcc.sds.base.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.dhcc.sds.base.dao.SdsQcentityItemLogMapper;
import com.dhcc.sds.base.entity.SdsQcentityItemEntity;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.sds.base.service.ISdsQcentityItemLogService;
import com.dhcc.sds.base.entity.SdsQcentityItemLogEntity;
import org.springframework.transaction.annotation.Transactional;

/**
 * 质控项目操作日志表表数据服务层接口
 * 
 * @ClassName: SdsQcentityItemLogServiceImpl
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-04-09 10:05:31
 */
@Service("sdsQcentityItemLogService")
public class SdsQcentityItemLogServiceImpl extends BaseServiceImpl<SdsQcentityItemLogMapper, SdsQcentityItemLogEntity> implements ISdsQcentityItemLogService {

	@Override
	public List<SdsQcentityItemLogEntity> page(Page<SdsQcentityItemLogEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<SdsQcentityItemLogEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void insertOperLog(Map<String, Object> param){
		Integer type = (Integer) param.get("type");
		String username = param.get("username").toString();
		Date updateDate = (Date) param.get("updateDate");
		SdsQcentityItemEntity newData = (SdsQcentityItemEntity) param.get("newData");
		SdsQcentityItemEntity oldData = (SdsQcentityItemEntity) param.get("oldData");

		SdsQcentityItemLogEntity logEntity = new SdsQcentityItemLogEntity();
		logEntity.setOperUser(username);
		logEntity.setOperTime(updateDate);
		logEntity.setOperType(type);

		StringBuffer content = new StringBuffer();
		switch (type){
			case 1:
				logEntity.setEntityId(newData.getEntityId());
				logEntity.setEntityItemId(newData.getId());
				content.append(newData.getBtCode()+"("+newData.getBtDesc()+") ");
				logEntity.setOperContent(content.append("新增").toString());
				break;
			case 2:
				logEntity.setEntityId(oldData.getEntityId());
				logEntity.setEntityItemId(oldData.getId());
				content.append(newData.getBtCode()+"("+newData.getBtDesc()+") ");
				if (!newData.getBtCode().equals(oldData.getBtCode())) {
					content.append("#项目代码");
				}
				if (!newData.getBtDesc().equals(oldData.getBtDesc())) {
					content.append("#项目描述");
				}
				if (!newData.getBtUpType().equals(oldData.getBtUpType())) {
					content.append("#上传类型");
				}
				if (!newData.getBtIsActive().equals(oldData.getBtIsActive())) {
					content.append("#有效标志");
				}
				if (!newData.getBtIsNeeded().equals(oldData.getBtIsNeeded())) {
					content.append("#是否必填");
				}
				if (!newData.getFormItemCode().equals(oldData.getFormItemCode())) {
					content.append("#关联表单项目");
				}
				content.append(" 修改");
				logEntity.setOperType(type);
				logEntity.setOperContent(content.toString());
				break;
			case 3:
				logEntity.setEntityId(oldData.getEntityId());
				logEntity.setEntityItemId(oldData.getId());
				content.append(oldData.getBtCode()+"("+oldData.getBtDesc()+") ");
				logEntity.setOperContent(content.append("作废").toString());
				break;
			default:
				break;
		}
		this.baseMapper.insert(logEntity);
	}

}